<?php
/**
 * 角色管理模型
 * @author sundongdong
 * @date 2016-07-06
 */
class System_Model_Role extends Model_Base {
	/**
	 * 获取角色列表
	 * @param array $param
	 */
	public function getRoleList(array $params) {
		$where = " where 1 ";
		$datas = array();
		if($params['search_type']!='' && $params['keyword']!=''){
			$where .=" and {$params['search_type']} like :keyword";
			$datas['keyword'] = '%'.$params['keyword'].'%';
		}
		$countSql = "SELECT count(*) from s_role" . $where;
		$totalCounts = $this->db()->fetchOne($countSql,$datas);
		$sql = "SELECT * FROM s_role " . $where . ' limit '.($params['page']-1)*$params['perPage'].','.$params['perPage'] ;
		$result['lists'] = $this->db()->fetchAll($sql,$datas);
		$result['totalCounts'] = $totalCounts;
		return $result;
	}
	/**
	 * 添加/更新 角色
	 * @param array $data
	 * @param unknown $user
	 */
	public function replaceRole(array $data, $user) {
		try {
			$existId = $this->existRole($data['role_name']);
			if ($existId && $existId != $data['role_id']) {
				return array("opr" => 0, "content" => "角色名不允许重复，请重新填写！");
			}
			if (!empty($data['role_id'])) {
				$data['update_user'] = $user;
				$this->db('write_db')->update('s_role', $data, 'role_id='.$data['role_id']);
			} else {
				$data['create_user'] = $user;
				$data['create_time'] = date('Y-m-d H:i:s');
				$this->db('write_db')->insert('s_role', $data);
			}
			return array("opr"=>1,"content"=>"操作成功");
		} catch (Exception $e) {
			return array("opr"=>0,"content"=>$e->getMessage());
		}
	}
	/**
	 * 角色查重
	 * @param unknown $role_name
	 * @return string
	 */
	public function existRole($role_name) {
		return $this->db()->fetchOne("select role_id from s_role where role_name=:name", array('name'=>$role_name));
	}
	/**
	 * 根据ID获取角色信息
	 * @param number $role_id
	 */
	public function getRoleById($role_id=0) {
		return $this->db()->fetchRow("select * from s_role where role_id=:id", array('id'=>$role_id));
	}
	/**
	 * 删除角色
	 * @param number $role_id
	 */
	public function delRole($role_id=0) {
		try{
			$this->db('write_db')->delete('s_role', "role_id=".$role_id);
			return array("opr"=>1,"content"=>"删除成功");
		}catch(Exception $e){
			return array("opr"=>0,"content"=>$e->getMessage());
		}
	}
	/**	
	 * 获取角色权限
	 * @param number $role_id
	 */
	public function getRolePrivilege($role_id=0) {
		return $this->db()->fetchAll("select * from `s_auth` where role_id = :role_id", array('role_id' => $role_id));
	}

	/**
	 * 根据用户ID取他可操作的菜单ID
	 * @QiuXiangCheng
	 * @param $uid 用户ID
	 * @return array()
	 */
	public function getAuthByUserId($uid){

		$menuId = $this -> db() -> fetchAssoc('SELECT element_id FROM s_auth sa WHERE role_id IN(SELECT role_id FROM s_user_role WHERE user_id = ' . $uid . ')');
		return array_keys($menuId);
	}

	public function getUserRoleIds($uid){

		return array_keys($this -> db() -> fetchAssoc('SELECT role_id FROM s_user_role WHERE user_id = ' . $uid));
	}

	/**
	 * 判断用户是否对指定的菜单有操作权限
	 * @QiuXiangCheng
	 * @param 用户ID
	 * @param 菜单ID
	 * @return boolean
	 */
	public function can($uid, $mid){

		if(in_array($mid, $this -> getAuthByUserId($uid), true)){
			return true;
		}
		return false;
	}

	/**
	 * 更新角色权限
	 * @param unknown $role_id
	 * @param unknown $privileges
	 * @param unknown $user
	 */
	public function updatePrivilege($role_id, $privileges, $user) {
		$this->db("write_db")->beginTransaction();
		try {
			$this->db("write_db")->delete('s_auth', 'role_id='.$role_id);
			if (count($privileges) > 0) {
				foreach ($privileges as $privilege) {
					$params = array();
					$params['role_id'] = $role_id;
					$params['element_id'] = $privilege;
					$params['update_user'] = $user;
					$this->db("write_db")->insert('s_auth', $params);
				}
			}
			$this->db("write_db")->commit();
			return true;
		} catch (Exception $e) {
			$this->db("write_db")->rollBack();
			return false;
		}
	}
}